package com.wechat.order.service.PayService.Impl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.wechat.order.dto.OrderDTO;
import com.wechat.order.service.orderService.OrderService;
import com.wechat.order.service.PayService.PayService;
import com.wechat.order.util.JsonUtil;
import com.wechat.order.util.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信支付
 * Created by zzy on 2017/12/18.
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME="微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService;

    /**
     * 传递参数，请求微信接口生成预付单，返回参数
     * @param orderDTO
     * @return
     */
    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest=new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信支付, request={}]", JsonUtil.toJson(payRequest));
        PayResponse payResponse= bestPayService.pay(payRequest);
        log.info("[微信支付],payRespone={}",JsonUtil.toJson(payResponse));
        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1、验证签名
        //2、支付的状态
        //3、支付的金额
        //4、支付人(下单人==支付人)
        PayResponse payResponse= bestPayService.asyncNotify(notifyData);
        log.info("[微信支付, request={}]", JsonUtil.toJson(payResponse));
        //查询订单
        String orderId= payResponse.getOrderId();
        OrderDTO dto=orderService.findOne(orderId);
        //判断订单是否存在
        if(dto==null){

        }
        //判断金额是否一致(0.10,0.1)//dto.getOrderAmount().compareTo(new BigDecimal(payResponse.getOrderAmount()))==0
        if(MathUtil.equels(payResponse.getOrderAmount(),dto.getOrderAmount().doubleValue())){

        }

        //修改订单支付状态

        orderService.paid(orderId);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest=new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信退款] request={}",JsonUtil.toJson(refundRequest));
        RefundResponse refundResponse= bestPayService.refund(refundRequest);
        log.info("[微信退款] response={}",JsonUtil.toJson(refundResponse));

        return refundResponse;
    }
}
